package com.yq.cp.controller.basics;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.SecureManagement;
import com.yq.cp.entity.basics.SecureTrainingRecords;
import com.yq.cp.entity.basics.model.SecureListExcel;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.service.basics.SecureTrainingRecordsService;
import com.yq.cp.utils.BeanCopyUtil;
import com.yq.cp.utils.DateTimeUtil;
import com.yq.cp.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 安全培训记录管理 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/secureTrainingRecords")
public class SecureTrainingRecordsController {
    @Resource
    SecureTrainingRecordsService secureTrainingRecordsService;

    /**
     * 添加编辑
     * @param secureTrainingRecords
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody SecureTrainingRecords secureTrainingRecords){
        if (Objects.isNull(secureTrainingRecords.getId()))
            secureTrainingRecords.setCreateUserId(UserThreadLocal.get().getId());
        secureTrainingRecordsService.saveOrUpdate(secureTrainingRecords);
        return ApiResult.success();
    }

    /**
     * 列表
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        Page<SecureTrainingRecords> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<SecureTrainingRecords> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("department_name",vo.getLike());
        if (!Objects.isNull(vo.getUsername())&&!"".equals(vo.getUsername()))
            wrapper.like("teacher_name",vo.getUsername());
        if (!Objects.isNull(vo.getBeginTime())&&!"".equals(vo.getBeginTime())) {
            String strDateTime3 = DateTimeUtil.getStrDateTime3(vo.getBeginTime());
            wrapper.like("time",strDateTime3);
        }

        wrapper.eq("site_id",vo.getSiteId());
        wrapper.orderByDesc("create_time");
        page = secureTrainingRecordsService.page(page,wrapper);
        return ApiResult.success(page);
    }

    /**
     * 删除
     * @param secureManagement
     * @return
     */
    @PostMapping("remove")
    public ApiResult remove(@RequestBody SecureTrainingRecords secureManagement){
        secureTrainingRecordsService.removeById(secureManagement);
        return ApiResult.success();
    }

    /**
     * 列表导出
     * @param vo
     * @return
     */
    @PostMapping("listExport")
    public void listExport(@RequestBody PageVo vo){
        QueryWrapper<SecureTrainingRecords> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("department_name",vo.getLike());
        if (!Objects.isNull(vo.getUsername())&&!"".equals(vo.getUsername()))
            wrapper.like("teacher_name",vo.getUsername());
        if (!Objects.isNull(vo.getBeginTime())&&!"".equals(vo.getBeginTime()))
            wrapper.like("time",vo.getBeginTime());
        wrapper.eq("site_id",vo.getSiteId());
        wrapper.orderByDesc("create_time");
        List<SecureTrainingRecords> list = secureTrainingRecordsService.list(wrapper);
        ArrayList<SecureListExcel> excels = new ArrayList<>();
        for (SecureTrainingRecords record : list) {
            SecureListExcel secureListExcel = new SecureListExcel();
            secureListExcel.setTitle(record.getTitle());
            secureListExcel.setAddress(record.getAddress());
            secureListExcel.setDepartmentName(record.getDepartmentName());
            secureListExcel.setTeacherName(record.getTeacherName());
            secureListExcel.setCreateTime(DateTimeUtil.getStrDateTime(record.getCreateTime()));
            secureListExcel.setTime(DateTimeUtil.getStrDateTime(record.getTime()));
            String str = "";
            if (record.getMethod().contains("1"))
                str = str + " " + "现场讲解";
            if (record.getMethod().contains("2"))
                str = str + " " + "视频学习";
            if (record.getMethod().contains("3"))
                str = str + " " + "到场演练";
            if (record.getMethod().contains("4"))
                str = str + " " + "实际学习";
            secureListExcel.setMethod(str);
            excels.add(secureListExcel);
        }
        //List<SecureListExcel> excels = BeanCopyUtil.copyList(list, SecureListExcel.class);
        ExcelUtil.doWrite("安全列表","",SecureListExcel.class,excels);

    }


    /**
     * 详情导出
     * @param id
     * @param response
     * @throws Exception
     */
    @GetMapping("exportExcel/{id}")
    public void exportExcel(@PathVariable Long id, HttpServletResponse response) throws Exception {
        secureTrainingRecordsService.exportExcel(id,response);

    }

}

